# -*- coding: utf-8 -*-
from flask import Flask ,render_template,session, request
from flask_socketio import SocketIO, emit

#推送库
from igetui.igt_message import IGtAppMessage
from igetui.template.igt_link_template import LinkTemplate
from igt_push import IGeTui

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
socketio = SocketIO(app)


APPID = 'QClyL7cxcmAvDUOYIgzKw3';
APPKEY = 'VwS4IS9pcP9OrnMbrgApH4';
MASTERSECRET = 'eX0qlvmBlP7BSAuFxgL9C8';
# CID = 'e560b884d8d9bf5bc5a0f9da545a11f3';
HOST = 'http://sdk.open.api.igexin.com/apiex.htm'

def pushMessageToApp():
    push = IGeTui(HOST, APPKEY, MASTERSECRET)

    # 新建一个推送模版, 以链接模板为例子，就是说在通知栏显示一条含图标、标题等的通知，用户点击可打开您指定的网页
    template = LinkTemplate()
    template.appId = APPID
    template.appKey = APPKEY
    template.title = u"欢迎使用个推!"
    template.text = u"这是一条推送消息~"
    template.logo = ""
    template.url = "http://www.baidu.com"
    template.transmissionType = 1
    template.transmissionContent = ''
    template.isRing = True
    template.isVibrate = True
    template.isClearable = True

    #定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
    message = IGtAppMessage()
    message.data = template
    message.isOffline = True
    message.offlineExpireTime = 1000 * 600
    message.appIdList.extend([APPID])

    push.pushMessageToApp(message)
    # print (ret)


@app.route('/')
def index():
    return render_template('index.html')

@socketio.on('my_ping', namespace='/test')
def ping_pong():
    emit('my_pong')

@socketio.on('my_pushevent',namespace='/test')
def push_event():
    pushMessageToApp()


if __name__ == '__main__':
    socketio.run(app)




